package com.ophiux.wechatpay.util;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;


public class AxisUtil {
	
	public static void main(String[] args) {
//		micropay("");
//		refund();
		smpay("");
//		closeOrder("");
//		queryOrderResult("");
	}

	public static String smpay(String param) {
		param = "{\"mch_token\":\"5998e05f-009c-4efb-a90c-1122748a5a70\",\"device_info\":\"2\",\"nonce_str\":\"3\",\"sign\":\"123\",\"sign_type\":\"MD5\",\"body\":\"扫码支付\",\"out_trade_no\":\"4004512001201706308140842224\",\"fee_type\":\"CNY\",\"total_fee\":\"1\",\"pay_type\":\"wechat\",\"spbill_create_ip\":\"192.168.6.89\",\"time_start\":\"20171205160000\",\"time_expire\":\"20181205170500\",\"limit_pay\":\"\",\"cashier\":\"何海龙\",\"attach\":\"暂无\",\"method\":\"6\"}";
		param = "{\"sign\": \"2DC9FE54CA82980478CF470917ED8C95\", \"mch_id\": \"23010101070107120500101\", \"body\": \"doPay\",\"out_trade_no\": \"we20191113220822\",\"total_fee\": \"14\",\"spbill_create_ip\": \"127.0.0.1\",\"cashier\": \"00000\",\"attach\": \"0\"}";
		String result = invoke(param, "frushFacePay");
		return result;
	}
	
	public static String refund(String param) {
//		String param = "{'mch_token':'b05f2a23-3286-459a-9ed3-599c9a8b93b0','nonce_str':'3',"
//				+ "'out_trade_no':'4004512001201706308140844446',"
//				+ "'out_refund_no':'4004512001201706308140844446',"
//				+ "'transaction_id':'','total_fee':'300','refund_fee':'300',"
//				+ "'refund_fee_type':'CNY','sign':'no_credit'}";
		String result = invoke(param, "refund");
		return result;
	}
	
	public static String micropay(String param) {
		param = "{\"mch_token\": \"5998e05f-009c-4efb-a90c-1122748a5a70\", \"device_info\": \"�����շѴ���\", \"nonce_str\": \"20180930101219\",\"sign\": \"44B215C8BC3B45F43BBDB6055918F5FE\",\"sign_type\": \"MD5\",\"body\": \"�����շ�ɨ��֧��\",\"out_trade_no\": \"20180930101219\",\"fee_type\": \"CNY\",\"total_fee\": \"3\",\"pay_type\":\"wechat\",\"spbill_create_ip\": \"127.0.0.1\",\"limit_pay\": \"no_credit\",\"auth_code\":\"134764596534314078\",\"cashier\":\"ϵͳ����\",\"attach\":\"����\",\"method\":\"1\"}";
		String result = invoke(param, "micropay");
		return result;
	}
	
	public static String closeOrder(String param) {
//		param = "{'mch_token':'NCQNRJUGSasdfsdf1231221Y16DU75BQN0P','nonce_str':'3213123123',"
//				+ "'out_trade_no':'134644663316232096','sign':'no_credit'}";
		String result = invoke(param, "closeOrder");
		return result;
	}
	
	public static String queryOrderResult(String param) {
		param = "{'mch_token':'5998e05f-009c-4efb-a90c-1122748a5a70','nonce_str':'3213123123',"
				+ "'out_trade_no':'4004512001201706308140844446','transaction_id':'','sign':'3079902C53AF736EEB1A8AD3150034AF','method':'1'}";
		return invoke(param, "queryOrderResult");
	}
	
	public static String queryOrder(String param) {
		String result = invoke(param, "queryOrder");
		return result;
	}
	
	public static String invoke(String param,String method) {
		String result="";
		try {
			String endpoint = PropertyUtil.getResWxValue("url");
			String spaceName = PropertyUtil.getResWxValue("spaceName");
			
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(endpoint);
			QName qname = new QName(spaceName, method);
			call.setOperationName(qname);
			call.addParameter("data", XMLType.XSD_STRING, ParameterMode.IN);
			Object[] obj = { param };
			call.setReturnType(XMLType.XSD_STRING);
			result = (String) call.invoke(obj);
			System.out.println(result);
		} catch (Exception e) {
			System.err.println(e.toString());
		}
		return result;
	}
}
